<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/3/29
 * Time: 9:55
 */

namespace App\Http\Controllers\VipChannel\Channel;

use App\Http\Controllers\VipChannel\BaseController;
use App\Http\Controllers\VipChannel\Channel\Transformers\ChannelDataInfoTransformer;
use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use Illuminate\Http\Request;

class ChannelController extends BaseController
{
    public function getChannelInfo(Request $request)
    {
        $data = [];
        $channel_user_ids = $this->getChannelUsers($request);
        foreach ($channel_user_ids as $channel_user_id) {
            $res = [];
            $channel_user = ChannelUser::getById($channel_user_id);
            $res['id'] = $channel_user->phone;
            $res['info'] = $channel_user->phone . '(' . $channel_user->nickname . ')';
            $channel_user_ids = explode(',', $channel_user_id);
            $distribution_channels = ChannelUser::getChannelInfosByChannelUserIds($channel_user_ids);
            foreach ($distribution_channels as $item) {
                $item->info = $item->id . '(' . $item->site_nick_name . ')';
            }
            $res['children'] = $distribution_channels;
            $data[] = $res;
        }
        return response()->success($data);
    }

    public function getDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        $distribution_channel_ids = $this->getDistributionChannelIds($request);
        if ($distribution_channel_ids && count($distribution_channel_ids) > 0) {
            $distribution_channel_id = min($distribution_channel_ids);
            //默认显示最小的站点的最近一个月的数据
            $params = compact('distribution_channel_id', 'start_date', 'end_date', 'date');
            $result = Channel::getChannelDayData($params, false);
            return response()->pagination(new ChannelDataInfoTransformer(), $result);
        } else {
            return response()->error('NOT_EXIST');
        }
    }

    public function exportDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        $distribution_channel_ids = $this->getDistributionChannelIds($request);
        if ($distribution_channel_ids && count($distribution_channel_ids) > 0) {
            $distribution_channel_id = min($distribution_channel_ids);
            //默认显示最小的站点的最近一个月的数据
            $params = compact('distribution_channel_id', 'start_date', 'end_date', 'date');
            $result = Channel::getChannelDayData($params, true);
            $header = ['日期', '分日充值总额', '普通充值人数', '包年人数', '成功订单数', '新注册用户', '新用户关注', '老用户关注', '新注册用户充值'];
            $data = [];
            foreach ($result as $item) {
                $data[] = array(
                    $item->date,
                    $item->total_recharge_amount,
                    $item->pay_success_user_num,
                    $item->year_recharge_user_num,
                    $item->paid_num,
                    $item->register_user_num,
                    $item->subscribe_user_num,
                    $item->old_user_subscribe_num,
                    $item->reg_user_first_recharge_amount
                );
            }
            saveExcelData($header, $data, '', '', '分日数据');
        }
    }

    public function getCatalogDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $type = $request->has('type') ? $request->input('type') : 'site';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        //此处 channel_user_id 其实为 phone
        $phone = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $groupBy = $type;
        $distribution_channel_ids = $this->getDistributionChannelIds($request);

        if ($phone) {
            \Log::info('getByPhone');
            $channel_user = ChannelUser::getByPhone($phone);
            if ($channel_user) {
                $distribution_channel_ids = ChannelUser::getChannelIdArray($channel_user->id);
            }
        }

        if ($distribution_channel_id) {
            $distribution_channel_ids = explode(',', $distribution_channel_id);
        }

        \Log::info($distribution_channel_ids);
        if (count($distribution_channel_ids) <= 0) {
            return response()->error('NOT_EXIST');
        } else {
            $params = compact('distribution_channel_ids', 'date', 'groupBy', 'start_date', 'end_date');
            $result = Channel::getChannelDayData($params, false);
            $isSiteType = ($type == 'site');
            foreach ($result as $item) {
                if ($isSiteType) {
                    $item->distribution_channel_id = $item->distribution_channel_id . '(' . $item->site_nick_name . ')';
                } else {
                    $item->phone = $item->phone . '(' . $item->channel_user_nickname . ')';
                }
            }
            return response()->pagination(new ChannelDataInfoTransformer(), $result);
        }
    }

    public function exportCatalogDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $type = $request->has('type') ? $request->input('type') : 'site';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        //此处 channel_user_id 其实为 phone
        $phone = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $groupBy = $type;
        $distribution_channel_ids = $this->getDistributionChannelIds($request);

        if ($phone) {
            $channel_user = ChannelUser::getByPhone($phone);
            if ($channel_user) {
                $distribution_channel_ids = ChannelUser::getChannelIdArray($channel_user->id);
            }
        }

        if ($distribution_channel_id) {
            $distribution_channel_ids = explode(',', $distribution_channel_id);
        }
        if (count($distribution_channel_ids) <= 0) {
            $result = [];
        } else {
            $params = compact('distribution_channel_ids', 'date', 'groupBy', 'start_date', 'end_date');
            $result = Channel::getChannelDayData($params, true);
        }

        $header = ['日期', ($type == 'account') ? '账号' : '站点','名称', '分日充值总额', '普通充值人数', '包年人数', '成功订单数', '新注册用户', '新用户关注', '老用户关注', '新注册用户充值'];
        $data = [];
        foreach ($result as $item) {
            $data[] = array(
                $item->date,
                ($type == 'account') ? $item->phone : $item->distribution_channel_id,
                ($type == 'account') ? $item->channel_user_nickname : $item->site_nick_name,
                $item->total_recharge_amount,
                $item->pay_success_user_num,
                $item->year_recharge_user_num,
                $item->paid_num,
                $item->register_user_num,
                $item->subscribe_user_num,
                $item->old_user_subscribe_num,
                $item->reg_user_first_recharge_amount
            );
        }
        saveExcelData($header, $data, '', '', '分日数据');
    }
}